﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.Drawing;
using LowLevelGraphics.Drawing.FloodFillModes;
//TODO: FloodFillRegions, Test


namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This does flood fills in defined seed points.
    /// Each floodfill is done from a seedpoint
    /// </summary>
    public class FloodFillRegions : BaseImageFilter
    {
        protected List<Point> m_aPoint = new List<Point>();

        protected AbstractFloodFill m_AbstractFloodFill = new FloodFillMode();
        /// <summary>
        /// Initializes a new instance of the <see cref="FloodFillRegions"/> class.
        /// </summary>
        public FloodFillRegions()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FloodFillRegions"/> class.
        /// </summary>
        /// <param name="_aPoint">The _a point.</param>
        public FloodFillRegions(List<Point> _aPoint)
        {
            m_aPoint = _aPoint;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FloodFillRegions"/> class.
        /// </summary>
        /// <param name="_floodFillRegions">The _flood fill regions.</param>
        internal FloodFillRegions(FloodFillRegions _floodFillRegions)
        {
            this.m_aPoint = _floodFillRegions.m_aPoint;
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            UnsafeGraphics graphics = new UnsafeGraphics(bitmap);

            if (m_aPoint.Count == 0)
            {
                CreateDefaultSeed(bitmap, 10,10);
            }

            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;

            Color color = Color.Transparent;
            foreach (Point point in m_aPoint)
            {
                color = _bitmap.GetPixel(point.X, point.Y);
                m_AbstractFloodFill.Fill(_bitmap, point.X, point.Y, color);
            }
            graphics.Dispose();
            return _bitmap;
        }

        /// <summary>
        /// Creates the default seed.
        /// </summary>
        /// <param name="_bitmap">The _bitmap.</param>
        /// <param name="nXStep">The n X step.</param>
        /// <param name="nYStep">The n Y step.</param>
        private void CreateDefaultSeed(UnsafeBitmap _bitmap, int nXStep, int nYStep)
        {
            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;

            for (int y = 0; y < nHeight; y += nYStep)
            {
                for (int x = 0; x < nWidth; x += nXStep)
                {
                    m_aPoint.Add(new Point(x, y));
                }
            }
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new FloodFillRegions(this);
        }
    }
}
